import os

import writeExcel
import xlrd


def read_excel(path):
  for filename in os.listdir(path):  # 读取目录下所有文件和文件夹
    file_path = os.path.join(path, filename)
    if os.path.isfile(file_path):  # 判断当前路径是否为文件
      print(filename)
      print('文件名：%s' % filename)
      print('文件完整路径：%s\n' % file_path)
      dealwith_excel(file_path, path)


def dealwith_excel(file_path,path):
  # 打开excel表格
  data_excel = xlrd.open_workbook(file_path)
  # 获取所有sheet名称
  # names = data_excel.sheet_names()
  for i in range(12):
    # sheet名称作为新excel表名
    names = data_excel.sheet_names()[i]
    print(names)
    # 获取对应的sheet内容
    table = data_excel.sheets()[i]
    n_rows = table.nrows  # 获取该sheet中的有效行数
    n_cols = table.ncols  # 获取该sheet中的有效列数
    # 表头
    head = table.row_values(0, start_colx=0, end_colx=None)
    print(head)
    # 数据
    row_data = []
    for i in range(1, n_rows):
      row_data_i = table.row_values(i, start_colx=0, end_colx=None)
      row_data.append(row_data_i)
    print(row_data)
    writeExcel.write_data(names,head,row_data,path)
